package cn.whit.blog.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import cn.whit.blog.dao.TblogtypeDao;
import cn.whit.blog.dao.VoteDao;
import cn.whit.blog.domain.Tblogtype;
import cn.whit.blog.domain.User;
import cn.whit.blog.domain.Vote;
import cn.whit.blog.util.DynamicQuery;
import cn.whit.blog.util.JDBCUtils;
import cn.whit.blog.util.PageModel;
import cn.whit.blog.util.Parameter;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class TblogtypeDaoImpl implements TblogtypeDao {

	@Override
	public Tblogtype findById(String id) throws SQLException {
		String sql="select * from tblogtype where id=?";
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		return qr.query(sql, new BeanHandler<Tblogtype>(Tblogtype.class),id);
	}

	@Override
	public List<Tblogtype> findAll() throws SQLException {
		String sql="select * from tblogtype";
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		return qr.query(sql, new BeanListHandler<Tblogtype>(Tblogtype.class));
	}

	@Override
	public List<Tblogtype> findAllWithPageAndQuery(int startIndex, int pageSize, DynamicQuery query) throws Exception {
		String sql="select * from tblogtype where 1=1 ";
		query.setBaseSql(sql);
		sql = query.generateSql();
		sql += " order by id desc limit  ? , ? ";
        //如果想排序，自行在sql后添加   
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		
		ArrayList<Parameter> parameters = query.getParameters();
		Object[] objs = new Object[parameters.size() + 2];
		int i = 0;
		for (Parameter p : parameters) {
			objs[i] = p.getValue();
			i++;
		}
		objs[i] = startIndex;
		objs[i+1] = pageSize;
		return qr.query(sql, new BeanListHandler<Tblogtype>(Tblogtype.class),objs);
	}

	@Override
	public int findTotalRecordsAndQuery(DynamicQuery query) throws Exception {
		String sql="select count(*) from tblogtype where 1=1 ";
		query.setBaseSql(sql);
		sql = query.generateSql();
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		ArrayList<Parameter> parameters = query.getParameters();
		Object[] objs = new Object[parameters.size()];
		int i = 0;
		for (Parameter p : parameters) {
			objs[i] = p.getValue();
			i++;
		}
		Long num=(Long)qr.query(sql, new ScalarHandler(),objs);
		return num.intValue();
	}

	@Override
	public void save(Tblogtype tblogtype) throws SQLException {
		String sql="INSERT INTO tblogtype(title,memo) "
				+ " VALUES(?,?)";
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		Object[] params={tblogtype.getTitle(),tblogtype.getMemo()};
		qr.update(sql,params);
	}

	@Override
	public void update(Tblogtype tblogtype) throws SQLException {
		String sql="update tblogtype set title= ? ,memo =? WHERE id=?";
		Object[] params={tblogtype.getTitle(), tblogtype.getMemo(),tblogtype.getId()};
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		qr.update(sql,params);
	}

	@Override
	public void deleteById(String s) throws SQLException {
		String sql=" delete from tblogtype  WHERE id=? ";
		Object[] params={ s};
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		qr.update(sql,params);
	}

	@Override
	public PageModel findByPageAndQuery(int curNum, int pageSize, DynamicQuery query) {
	 
		return null;
	}
 


}
